package com.sunfeng.chap14;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;

public class Test1 {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InterruptedException {
        System.out.println("A");
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(Integer.MAX_VALUE);
        System.out.println("B");
        byte[] bytes = new byte[]{'a'};
        System.out.println(Integer.MAX_VALUE);
        for (int i = 0; i < Integer.MAX_VALUE; i++) {
            byteBuffer.put(bytes);
        }
        System.out.println("end");
        TimeUnit.SECONDS.sleep(1);
        Method cleanerMethod = byteBuffer.getClass().getMethod("cleaner");
        cleanerMethod.setAccessible(true);
        Object invoke = cleanerMethod.invoke(byteBuffer);
        Method cleanMethod = invoke.getClass().getMethod("clean");
        cleanMethod.setAccessible(true);
        cleanMethod.invoke(invoke);

    }
}
